﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TSJHVA06
    {

        public TSJHVA06()
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            this.DataContext = MV;
            InitializeComponent();

        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataTable tb = TBMain.Clone();
            string sind = "", ART = "";
            if (dwMaster == null)
                return;


            ART = dwMaster["VAADART"].ToString();
            dwData = TMV.VWS.TBMain.NewRow();
            dwData["WAMANDANT"] = app.Mandant;
            dwData["WAART"] = ART;
            SResult = TMV.VWS.QueryWASTAMM(dwData, 12);
            Set.Merge(SResult);

            tb.TableName = "VAART";
            Set.Merge(tb);
            cDR.DocType = VALBTYP;
            cDR.PRINT(Set);
        }
        private void btnPrintVA06_Click(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataTable tb = TBMain.Clone();
            string sind = "", ART = "";
            Set.Merge(TMV.SetProd);
            cDRVA06.PRINT(Set);
        }
        public override void ImportExcel(object sender, RoutedEventArgs e)
        {
            if (cDR == null)
                return;
            try
            {
                cDR.DocType = "LOADEXCEL";
                // cDR.Root = @"C:\IntexReport\Data\KD\EXCEL"; 
                cDR.DESTFOLDER = @"C:\IntexReport\Data\VA\EXCEL";
                if (!System.IO.Directory.Exists(cDR.DESTFOLDER))
                {
                    throw new Exception(cDR.DESTFOLDER + " 不存在");
                }
                cDR.PRINT(null);
                SResult = cDR.SetExchange;
                if (SResult.Tables.Count > 0)
                {
                    ExVA(SResult);
                    ExVAF(SResult);
                    ExWAF(SResult);
                    ExWS(SResult);
                    ExWS22(SResult);
                    ExWS33(SResult);
                    ExWS31(SResult);
                    ExBS03(SResult);
                    ExBS04(SResult);
                    ExBS07(SResult);
                    ExBS08(SResult); 
                }
                else
                {
                    MessageBox.Show("确认存在" + @"C:\IntexReport\Data\ITX\EXCEL\ITXEX-XXXX.xls");
                }
                QueryCommand(null, null);
                vwMaster.View.MoveCurrentToFirst();
                while (true)
                {
                    vwMaster.View.MoveCurrentToNext();
                    if (vwMaster.View.IsCurrentAfterLast)
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            } 
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsReady)
            {
               string mandant = app.Mandant;
                app.Mandant = "99";
                QueryCommand(null, null);
                app.Mandant = mandant;
            }
            base.NewCommand(sender, e);
        }
        public void ExVA(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString());
                dwData["VAAKOLL"] = "SJH17";
                dwData["VAAARTST"] = "F";
                dwData["VAAART"] = Helper.Left(art.PadRight(24), 24);
                dwData["VAADART"] = Helper.Left(art.PadRight(20), 20);
                dwData["VAABEZ"] = Helper.Left(des.Trim().PadRight(30), 30);
                //  dwData["VAAFILLER"] = Helper.Left(row["COMPOSITION"].ToString().Trim().PadRight(72), 72);
                //dwData["VAAGEWVON"] = double.Parse(row["WEIGHT"].ToString()) * 100;
                //  //dwData["VAABRTBIS"] = double.Parse(row["WIDTH"].ToString()) * 100;  
                //     dwData["VAAZOLLTNR1"] = Helper.Left(row["CODE"].ToString().Trim().PadRight(20), 20);
                //    dwData["VAAREFART"] = Helper.Left(row["LOCATION"].ToString().Trim().PadRight(24), 24);
                //    dwData["VAAARTFAM"] = Helper.Left(row["BOOK"].ToString().Trim().PadRight(24), 24);
                //     dwData["VAAARTWEB"] = Helper.Left(row["CUSTOMER PRICE"].ToString().Trim().PadRight(24), 24);
                dwData["VAAAENDBEN"] = app.User;
                dwData["VAAAENDDAT"] = TMV.date;
                TMV.BATCHWB14(dwData);
            }
            TMV.POSTCHG();
        }
        public void ExVAF(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VAF.TBMain.NewRow();
            TMV.VAF.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString());
                dwData["VAFKOLL"] = "SJH17"; 
                dwData["VAFART"] = Helper.Left(art.PadRight(24), 24);
                //dwData["VAFART"] = ART;
                dwData["VAFFARBE"] = "1".PadRight(10).Substring(0, 10);
                dwData["VAFPART"] = dwData["VAFART"].ToString().Substring(0, 20);
                dwData["VAFPLART"] = dwData["VAFART"].ToString().Substring(0, 20);
                dwData["VAFPART"] = dwData["VAFART"].ToString().Substring(0, 20);
                dwData["VAFPVAR"] = "  ";
                TMV.VAF.BATCHWB14(dwData);
            }
            TMV.VAF.POSTCHG();
        }
        public void ExWS(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.TBMain.NewRow();
            TMV.VWS.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString());
                dwData["WAMANDANT"] = app.Mandant;
                dwData["WAFKZ"] = "J";
                dwData["WAART"] = Helper.Left(art.PadRight(20), 20);
                dwData["WAVAR"] = "  ";
                dwData["WABEZ"] = Helper.Left(des.Trim().PadRight(40), 40);
                dwData["WAFDDIM"] = "INC";
                dwData["WARFDDIS"] = Double.Parse(row["WDENX"].ToString()) * 100;
                dwData["WARWBRT"] = Double.Parse(row["RWIDTHRT"].ToString()) * 10;
                dwData["WARBRTOL"] = Double.Parse(row["RWIDTH"].ToString()) * 10;
                TMV.VWS.BATCHWB14(dwData);
            }
            TMV.VWS.POSTCHG();
        }

        public void ExWAF(DataSet db)
        {
            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VWF.TBMain.NewRow();
            TMV.VWS.VWF.SetUpNewRow(ref dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString());
                dwData["WAFART"] = art.PadRight(20).Substring(0, 20);
                dwData["WAFVAR"] = "  ";
                dwData["WAFBEZ"] = Helper.Left(des.Trim().PadRight(20), 20);
                dwData["WAFZUART"] = "C";
                dwData["WAFZUSATZ"] = "1".PadRight(10).Substring(0, 10);
                TMV.VWS.VWF.BATCHWB14(dwData);
            }
            TMV.VWS.VWF.POSTCHG();
        }
        public void ExWS33(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWA.TBMain.NewRow();
            TMV.VWA.SetUpNewRow(ref dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString()); 
                dwData["WAGART"] = art.PadRight(20).Substring(0, 20);
                dwData["WAGVAR"] = "  ";
                dwData["WAGWCOL"] = "1".PadRight(10).Substring(0, 10);
                string yarn = Helper.ConvertGB2312To8859P1(row["FYARN"].ToString());
                int idx = yarn.IndexOf("+"); 
                double qty = 0;
                if(idx<=0)
                {
                    //qty = Double.Parse(row["FUSE"].ToString()) * 0.1;
                    dwData["WAGPOS"] = "01";
                    dwData["WAGGART"] = yarn;
                    dwData["WAGGVAR"] = "".PadRight(3);
                    dwData["WAGGFARBE"] = "".PadRight(10);
                   // dwData["WAGKGNET"] = qty;
                    //dwData["WAGKGBRUT"] = qty * 1.003;
                    dwData["WAGABF"] = 30;
                    TMV.VWA.BATCHWB14(dwData);
                }
                else
                {
                   // string FUSE = Helper.ConvertGB2312To8859P1(row["FUSE"].ToString());
                  //  int idx1 = FUSE.IndexOf("/");
                    //qty = double.Parse(FUSE.Substring(0, idx));
                    dwData["WAGPOS"] = "01";
                    dwData["WAGGART"] = yarn.Substring(0,idx).PadRight(20).Substring(0,20);
                    dwData["WAGGVAR"] = "".PadRight(3);
                    dwData["WAGGFARBE"] = "".PadRight(10);
                    dwData["WAGABF"] = 30;
                   // dwData["WAGKGNET"] = qty;
                    //dwData["WAGKGBRUT"] = qty * 1.003;
                    TMV.VWS.VWS33.BATCHWB14(dwData);
                   // qty = double.Parse(FUSE.Substring(idx+1)) * 0.1;
                    dwData["WAGPOS"] = "02";
                    dwData["WAGGART"] = yarn.Substring(idx+1).PadRight(20).Substring(0, 20);
                    dwData["WAGGVAR"] = "".PadRight(3);
                    dwData["WAGGFARBE"] = "".PadRight(10);
                    dwData["WAGABF"] = 30;
                    //  dwData["WAGKGNET"] = qty;
                    //dwData["WAGKGBRUT"] = qty * 1.003;
                    TMV.VWA.BATCHWB14(dwData);
                }
              
               
            }
            TMV.VWA.POSTCHG();
        }
       
        public void ExBS03(DataSet db)
        { 
            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VBS.TBMain.NewRow();
            TMV.VWS.VBS.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString());
                dwData["BSMANDANT"] = app.Mandant; 
                dwData["BSKART"] = Helper.Left(art.PadRight(20), 20); 
                dwData["BSBEZ"] = Helper.Left(des.Trim().PadRight(40), 40); 
                dwData["BSFDZ"] = Double.Parse(row["WENDS"].ToString()); 
                TMV.VWS.VBS.BATCHWB14(dwData);
            }
            TMV.VWS.VBS.POSTCHG();
        }
        public void ExBS04(DataSet db)
        { 
            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VBH.TBMain.NewRow();
            TMV.VWS.VBH.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString());
                dwData["BHMANDANT"] = app.Mandant; 
                dwData["BHKART"] = Helper.Left(art.PadRight(20), 20);
                dwData["BHKVAR"] = "01"; 
                TMV.VWS.VBH.BATCHWB14(dwData);
            }
            TMV.VWS.VBH.POSTCHG();
        }
        public void ExBS08(DataSet db) 
        { 
            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VBAS.TBMain.NewRow();
            TMV.VWS.VBAS.SetUpNewRow(ref dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                string des = Helper.ConvertGB2312To8859P1(row["DES"].ToString()); 
                dwData["BAGART"] = art.PadRight(20).Substring(0, 20);
                dwData["BAGVAR"] = "  ";
                dwData["BAGFARBE"] = "1".PadRight(10).Substring(0, 10);
                string yarn = Helper.ConvertGB2312To8859P1(row["WYARN"].ToString());
                int idx = yarn.IndexOf("+");
                double qty = 0;
                if (idx == 0)
                {
                    //qty = Double.Parse(row["FUSE"].ToString()) * 0.1;
                    dwData["BAGPOS"] = "01";
                    dwData["BAGGART"] = yarn;
                    dwData["BAGGVAR"] = "".PadRight(3);
                    dwData["BAGGFARBE"] = "".PadRight(10);
                   // dwData["BAGKGNET"] = qty;
                   // dwData["BAGKGBRUT"] = qty * 1.003;
                    dwData["BAGABF"] = 30;
                    TMV.VWS.VBAS.BATCHWB14(dwData);
                }
                else
                {
                   // string FUSE = Helper.ConvertGB2312To8859P1(row["WUSE"].ToString());
                   // int idx1 = FUSE.IndexOf("/");
                   // qty = double.Parse(FUSE.Substring(0, idx));
                    dwData["BAGPOS"] = "01";
                    dwData["BAGGART"] = yarn.Substring(0, idx).PadRight(20).Substring(0, 20);
                    dwData["BAGGVAR"] = "".PadRight(3);
                    dwData["BAGGFARBE"] = "".PadRight(10);
                    dwData["BAGABF"] = 30;
                   // dwData["BAGKGNET"] = qty;
                   // dwData["BAGKGBRUT"] = qty * 1.003;
                    TMV.VWS.VBAS.BATCHWB14(dwData);
                   // qty = double.Parse(FUSE.Substring(idx + 1)) * 0.1;
                    dwData["BAGPOS"] = "02";
                    dwData["BAGGART"] = yarn.Substring(idx + 1).PadRight(20).Substring(0, 20);
                    dwData["BAGGVAR"] = "".PadRight(3);
                    dwData["BAGGFARBE"] = "".PadRight(10);
                    dwData["BAGABF"] = 30;
                   // dwData["BAGKGNET"] = qty;
                    //dwData["BAGKGBRUT"] = qty * 1.003;
                    TMV.VWS.VBAS.BATCHWB14(dwData);
                }
            }
            TMV.VWS.VBAS.POSTCHG();
        }
        public void ExBS07(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VBF.TBMain.NewRow();
            TMV.VWS.VBF.SetUpNewRow(ref dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                dwData["BSFMANDANT"] = app.Mandant;
                dwData["BSFKART"] = art.PadRight(20).Substring(0, 20);
                dwData["BSFKVAR"] = "01";
                dwData["BSFKFARBE"] = "1".PadRight(10).Substring(0, 10);
                TMV.VWS.VBF.BATCHWB14(dwData);
            }
            TMV.VWS.VBF.POSTCHG();
        }
        public void ExWS31(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VWS31.TBMain.NewRow();
            TMV.VWS.VWS31.SetUpNewRow(ref dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString(); 
                dwData["WAKMANDANT"] = app.Mandant;
                dwData["WAKART"] = art.PadRight(20).Substring(0, 20);
                dwData["WAKVAR"] = "  ";
                dwData["WAKWCOL"] = "1".PadRight(10).Substring(0, 10);
                dwData["WAKKART"] = art.PadRight(20).Substring(0, 20);
                dwData["WAKKVAR"] = "01";
                dwData["WAKKFARBE"] = "1".PadRight(10).Substring(0, 10);
                dwData["WAKPOS"] = "01";
                TMV.VWS.VWS31.BATCHWB14(dwData);  
            }
            TMV.VWS.VWS31.POSTCHG();
        }
        public void ExWS22(DataSet db)
        {


            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TMV.VWS.VWT.TBMain.NewRow();
            TMV.VWS.VWT.SetUpNewRow(ref dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = row["ARTICLE"].ToString();
                dwData["WTGMANDANT"] = app.Mandant;
                dwData["WTGART"] = art.PadRight(20).Substring(0, 20);
                dwData["WTGVAR"] = "  ";
                string reed = Helper.ConvertGB2312To8859P1(row["REED"].ToString());
                int idx = reed.IndexOf("*");
                if (idx <= 0) continue;
                dwData["WTGWBBRT"] = Double.Parse(reed.Substring(0, idx));
                dwData["WTGWBF"] = Double.Parse(reed.Substring(idx+1));
                dwData["WTGNFTOLM"] = Double.Parse(row["LOOMSPEED"].ToString());
                dwData["WTGNFTOLP"] = Double.Parse(row["EFFIENCY"].ToString())*100;
                TMV.VWS.VWT.BATCHWB14(dwData);
            }
            TMV.VWS.VWT.POSTCHG();
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                PM["KOLL"] = TMV.VAAKOLL;
                PM["SART"] = TMV.VAADART.Trim();
                PM["TART"] = TMV.ART.Trim();
                TMV.VWS.Copy(PM);
                TMV.VWS.VWF.Copy(PM);
                TMV.VAF.Copy(PM);
                TMV.VWS.VWT.Copy(PM);
                TMV.VWS.VWS31.Copy(PM);
                TMV.VWA.Copy(PM);
                TMV.VWS.VBS.Copy(PM);
                TMV.VWS.VBH.Copy(PM);
                TMV.VWS.VBF.Copy(PM);
                TMV.VWS.VBAS.Copy(PM);
                base.CopyCommand(sender, e);
            }
            catch(Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }
    }
}
